1
|
|
|
var utils = require('./utils'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base prototype that all other classes in this library extend |
5
|
|
|
* |
6
|
|
|
* @constructor |
7
|
|
|
*/ |
8
|
|
|
var Base = function(){ |
9
|
|
|
// Nothing to do here, yet. It is being called though by classes which extend |
10
|
|
|
// it in case we think of something to do in the future. |
11
|
|
|
}; |
12
|
|
|
|
13
|
|
|
Base._gedxClass = Base.prototype._gedxClass = 'GedcomX.Base'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Check whether the given object is an instance of this class. |
17
|
|
|
* |
18
|
|
|
* @param {Object} obj |
19
|
|
|
* @returns {Boolean} |
20
|
|
|
*/ |
21
|
|
|
Base.isInstance = function(obj){ |
22
|
|
|
return utils.isInstance(obj, this._gedxClass); |
23
|
|
|
}; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set the value of a property that is an array. The new array is copied |
27
|
|
|
* by calling the associated addMethod on each value in the array. |
28
|
|
|
* |
29
|
|
|
* @param {Array} array New array that will be saved (copied) |
30
|
|
|
* @param {String} prop Property name where the array is found |
31
|
|
|
* @param {String} addMethod Name of the add method for this data |
32
|
|
|
* @return {Function} Generated function to be added to the prototype |
33
|
|
|
*/ |
34
|
|
|
Base.prototype._setArray = function(array, prop, addMethod){ |
35
|
|
|
if(Array.isArray(array)){ |
36
|
|
|
this[prop] = []; |
37
|
|
|
var self = this; |
38
|
|
|
array.forEach(function(n){ |
39
|
|
|
self[addMethod](n); |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
return this; |
43
|
|
|
}; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add an object to an array at the given property. |
47
|
|
|
* |
48
|
|
|
* This method centralizes code for ensuring that the property is an array and |
49
|
|
|
* that the given data exists (isn't null or undefined). |
50
|
|
|
* This method is designed to be used internally. |
51
|
|
|
* |
52
|
|
|
* @param {Object} data |
53
|
|
|
* @param {String} arrayName |
|
|
|
|
54
|
|
|
* @param {Fucntion} constructor |
55
|
|
|
* @return {ExtensibleData} This object |
56
|
|
|
*/ |
57
|
|
|
Base.prototype._arrayPush = function(data, prop, constructor){ |
58
|
|
|
if(data){ |
59
|
|
|
if(constructor){ |
60
|
|
|
data = constructor(data); |
61
|
|
|
} |
62
|
|
|
if(!Array.isArray(this[prop])){ |
63
|
|
|
this[prop] = []; |
64
|
|
|
} |
65
|
|
|
this[prop].push(data); |
66
|
|
|
} |
67
|
|
|
return this; |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Internal helper method for constructing JSON when toJSON() is called. |
72
|
|
|
* |
73
|
|
|
* Recursively call toJSON on properties that will be serialized, call toJSON on |
74
|
|
|
* the parent prototype, merge the two resulting objects together, and remove |
75
|
|
|
* any remaining undefined properties. |
76
|
|
|
* |
77
|
|
|
* @param {Function} parent Parent prototype |
78
|
|
|
* @param {String[]} properties List of properties that will be serialized |
79
|
|
|
* @return {Object} |
80
|
|
|
*/ |
81
|
|
|
Base.prototype._toJSON = function(parent, properties){ |
82
|
|
|
return utils.removeEmpty( |
83
|
|
|
// toJSON MUST be called on all data before merging otherwise the merge method |
84
|
|
|
// will reaching into the class instances and extract data you might not want |
85
|
|
|
// to be serialized. The data will come out as a plain object and you will lose |
86
|
|
|
// the ability to detect that it hasn't been properly serialized. |
87
|
|
|
utils.merge( |
88
|
|
|
parent.prototype.toJSON.call(this), |
89
|
|
|
utils.toJSON(utils.pick(this, properties)) |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
}; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Export the object as JSON |
96
|
|
|
* |
97
|
|
|
* @return {Object} JSON object |
98
|
|
|
*/ |
99
|
|
|
Base.prototype.toJSON = function(){ |
100
|
|
|
return {}; |
101
|
|
|
}; |
102
|
|
|
|
103
|
|
|
module.exports = Base; |